package com.titanrise.crm.dao;

import com.titanrise.crm.common.utils.Maybe;
import com.titanrise.crm.domain.model.Role;
import com.titanrise.crm.domain.model.SysLoginLog;
import com.titanrise.crm.domain.model.User;
import com.titanrise.crm.domain.model.enums.BelongStatus;
import com.titanrise.crm.environment.annotation.Autowired;
import com.titanrise.crm.environment.annotation.Repository;
import com.titanrise.crm.mapper.RoleMapper;
import com.titanrise.crm.mapper.SysLoginLogMapper;
import com.titanrise.crm.mapper.UserMapper;

import java.util.Objects;
import java.util.Optional;

@Repository
public class IndexDao {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SysLoginLogMapper logMapper;

    public Maybe<Role> findRoleByUsername(String username) {
        Objects.requireNonNull(username);
        return Maybe.fromNullable(roleMapper.selectByUsername(username));
    }

    public Maybe<Role> findRoleByUsernameAndPassword(String username, String password) {
        Objects.requireNonNull(username);
        Objects.requireNonNull(password);
        return Maybe.fromNullable(roleMapper.selectByUsernameAndPassword(username, password));
    }

    public Maybe<User> findUserById(String userId) {
        Objects.requireNonNull(userId);
        return Maybe.fromNullable(userMapper.selectById(userId));
    }

    public int insertRole(Role role) {
        Objects.requireNonNull(role);
        return roleMapper.insertOne(role);
    }

    public int insertUser(User user) {
        Objects.requireNonNull(user);
        return userMapper.insertOne(user);
    }

    public int insertLoginLog(SysLoginLog loginLog) {
        Objects.requireNonNull(loginLog);
        return logMapper.insertOne(loginLog);
    }

}
